package com.lovo.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.lovo.bean.Store;
import com.lovo.service.IStoreService;
import com.lovo.service.serviceImpl.StoreServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StoreServlet",urlPatterns = "/store.do")
public class StoreServlet extends HttpServlet {
    private IStoreService iss = new StoreServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if (method == null || method.equals("")) {
            showAllStore(request, response);
        } else if (method.equals("getStoreById")) {
            getOneStoreById(request, response);
        }else if(method.equals("addStore")){
            addOneStore(request,response);
        }else if(method.equals("updateStore")){
            updateStore(request,response);
        }else if(method.equals("deleteStore")){
            deleteOneStore(request,response);
        }else if(method.equals("findStore")){
            queryOneStore(request,response);
        }else if(method.equals("uploadPic")){
            uploadPic(request,response);
        }
    }

    protected void showAllStore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int page=0,limit=0;
        String page1 = request.getParameter("page");
        String limit1 = request.getParameter("limit");
        if(page1!=null||page1.equals("")){
            page =Integer.parseInt(page1);
        }
        if(limit1!=null||limit1.equals("")){
            limit =Integer.parseInt(limit1);
        }
        PageInfo<Store> storeByPage = iss.getStoreByPage(page, limit);
        String json = new Gson().toJson(storeByPage);
        response.getWriter().write(json);
    }
    protected void getOneStoreById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("storeId");
        int storeId =0;
        if(id!=null){
            storeId =Integer.parseInt(id);
        }
        Store store = iss.selectByPrimaryKey(storeId);
        String json = new Gson().toJson(store);
        response.getWriter().write(json);
    }
    protected void addOneStore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //     storeName:"",
        //     storeLicense:"",
        //     storeAddress:"",
        //     storeLocation:"",
        //     storeBoss:"",
        //     storeTel:"",
        //     storeFeature:"",
        //     storeProportion:"",
        //     storeLevel:"",
        //     storePic:"",
        //     storeState:"申请中"
        String storeName = request.getParameter("storeName");
        String storeLicense = request.getParameter("storeLicense");
        String storeAddress = request.getParameter("storeAddress");
        String storeLocation = request.getParameter("storeLocation");
        String storeBoss = request.getParameter("storeBoss");
        String storeTel = request.getParameter("storeTel");
        String storeFeature = request.getParameter("storeFeature");
        String storeProportion = request.getParameter("storeProportion");
        String storeLevel = request.getParameter("storeLevel");
        String storePic = request.getParameter("storePic");
        String storeState = request.getParameter("storeState");
        Store s =new Store(storeName,storeLicense,storeAddress,
                storeLocation,storeBoss,storeTel,storePic,storeFeature,
                storeLevel,storeProportion,storeState);

        boolean b = iss.insertOneStore(s);
        if(b){
            response.getWriter().write("true");
        }else{
            response.getWriter().write("false");
        }
    }
    protected void updateStore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("storeId");
        int storeId =0;
        if(id!=null||id.equals("")){
            storeId =Integer.parseInt(id);
        }
        String storeName = request.getParameter("storeName");
        String storeLicense = request.getParameter("storeLicense");
        String storeAddress = request.getParameter("storeAddress");
        String storeLocation = request.getParameter("storeLocation");
        String storeBoss = request.getParameter("storeBoss");
        String storeTel = request.getParameter("storeTel");
        String storeFeature = request.getParameter("storeFeature");
        String storeProportion = request.getParameter("storeProportion");
        String storeLevel = request.getParameter("storeLevel");
        String storePic = request.getParameter("storePic");
        String storeState = request.getParameter("storeState");
        Store s =new Store(storeId,storeName,storeLicense,storeAddress,
                storeLocation,storeBoss,storeTel,storePic,storeFeature,
                storeLevel,storeProportion,storeState);

        boolean b = iss.updateByPrimaryKeySelective(s);
        if(b){
            response.getWriter().write("true");
        }
        else{
            response.getWriter().write("false");
        }
    }
    protected void deleteOneStore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String storeId = request.getParameter("storeId");
        int id =0;
        if(storeId!=null){
            id= Integer.parseInt(storeId);
        }
        boolean b = iss.deleteStore(id);
        if(b){
            response.getWriter().write("true");
        }else{
            response.getWriter().write("false");
        }
    }
    protected void queryOneStore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int page=0,limit=0;
        String page1 = request.getParameter("page");
        String limit1 = request.getParameter("limit");
        if(page1!=null||page1.equals("")){
            page =Integer.parseInt(page1);
        }
        if(limit1!=null||limit1.equals("")){
            limit =Integer.parseInt(limit1);
        }
        String queryParam = request.getParameter("queryParam");
//        List<Store> stores = iss.selectByQuery(queryParam);
        PageInfo<Store> storePageInfo = iss.selectPageByQuery(queryParam, page, limit);
        String s = new Gson().toJson(storePageInfo);
        System.out.println("s = " + s);
        response.getWriter().write(s);

    }

    protected void uploadPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String savePath =this.getServletContext().getContextPath()+"/upload";
        String realPath = this.getServletContext ().getRealPath ("/upload");
        System.out.println("realPath = " + realPath);
        System.out.println("savePath = " + savePath);
        File file = new File (realPath);
        if (!file.exists () && file.isDirectory ()){
            System.out.println ("realPath = " + realPath + "目录不存在，需要创建");
            file.mkdir ();
        }else{
            System.out.println("没进来。。。。");
        }

//        String file = request.getParameter("file");
//        System.out.println("file = " + file);
        response.getWriter().write("上传成功...");
    }


}
